/**
 * GMap control on details view
 * @author tjunghan, pschuerm (HSZ-T)
 */
jQuery(function ($) {
    // Context of map
    var $map = $('#map'),
        $map_container = $map.parent(),
        $showMap_button = $('#showMap'),
        isMapLoaded = false,
        isMapVisible = false,
        latlng = '',
        button_labels = {
            'show_map' : 'Show map',
            'hide_map' : 'Hide map'
        };
   
    if ($map_container.length !== 1) {
        return false;
    }

    $showMap_button.click(function () {
        var $this = $(this),
            href = '';
   
        if (isMapVisible) {
            $map_container.addClass('hidden');
            $this.text(button_labels.show_map);
            isMapVisible = false;
        } else {
            $map_container.removeClass('hidden');
            $this.text(button_labels.hide_map);
            isMapVisible = true;
        }

        if (!isMapLoaded) {
            href = $this.attr('href');
            latlng = href.split('?latlng=')[1].split(',');
            $map.height(parseInt($map.width() / 1.6, 10) + 'px');

            var markers = [];

            // Add my location if available (depends on availability of geo api)
            if ($('#my_location').length === 1) {
                markers.push({
                    latitude : $('input[name="lat"]', '#my_location').val(),
                    longitude : $('input[name="lng"]', '#my_location').val(),
                    icon: {
                        image : '/images/arrow.png',
                        shadow : false,
                        iconsize : [39, 34],
                        shadowsize : false,
                        iconanchor : [11, 34]
                    }

                });
            }

            markers.push({ latitude: latlng[0],
                              longitude: latlng[1] });

            $map.gMap({
                markers: markers,
                zoom: 15,
                scrollwheel: false
            });
            isMapLoaded = true;       
        }

        return false;
    });
}(jQuery));


/**
 * PhoneMenu
 * @author tjunghan, pschuerm (HSZ-T)
 */
jQuery(function ($) {

    var $ctx = $('ul.phoneMenu');

    $ctx.children('li').click(function () {

        href = $('a.title', $(this)).attr('href');

        location.href = href;
        return false;
    });
}(jQuery));


/**
 * Geo API
 * @author tjunghan, pschuerm (HSZ-T)
 */

jQuery(function ($) {
    if ($('#geoLocator').length != 1) {
        return false;
    }

    var $ctx = $('#geoLocator'),
        $form = $ctx.parents('form'),
        lat = null,
        lng = null; 

    $('#near_me', $ctx).attr('checked', '');

    $('#near_me', $ctx).click(function () {
        var $this = $(this);
        if ($this.get(0).checked === true) {
            $('input[type="submit"]', $form).hide();
            get_lat_lng();
            $('input[name="search_where"]', $form).attr('disabled', 'disabled');
        } else {
            $('input[type="submit"]', $form).show();
            $('input[name="search_where"]', $form).attr('disabled', null);
        }
    });

    function get_lat_lng() {
        if(geo_position_js.init()){
            $('.loader', $ctx).removeClass('hidden');
            geo_position_js.getCurrentPosition(success_callback,error_callback,{enableHighAccuracy:true,options:5000});
        }
        else{
            alert("Functionality not available");

        }
    }

    function callback_helper() {
        $('.loader', $ctx).addClass('hidden');
        $('input[type="submit"]', $form).show();
    }


    function success_callback(p)
    {
        //alert('lat='+p.coords.latitude.toFixed(2)+';lon='+p.coords.longitude.toFixed(2));
        lat = p.coords.latitude.toFixed(2);
        lng = p.coords.longitude.toFixed(2);

        if (lat === null || lng === null) {
            error_callback(p);
        } else {
            $('#lat', $ctx).val(lat);
            $('#lng', $ctx).val(lng);
            callback_helper();
        }


    }

    function error_callback(p)
    {
        alert('error='+p.message);
        callback_helper();
    }

}(jQuery));